/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-29 16:41:30
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-15 11:40:24
 * @FilePath: /react18-vite-admin/src/views/system/UsersManage/useTable.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { TableProps } from 'antd';
import { useEffect } from 'react';

import type { UserListItem } from '@/typings/api/user';
import Button from '@/components/ui/Button/Button';
import { useEnumStore } from '@/store';
import { immerable } from 'immer';
import { useImmer } from 'use-immer';
import { useUserList } from '@/service/biz/user/useUserList';
import { usePaginationHelper } from '@/service/app/usePaginationHelper';

class ListParam {
    [immerable] = true;
    timeStamp = Date.now();
    current = 1;
    size = 10;
    username = '';
    status?: 1 | 0;
    roleId?: number;
    departmentId?: number;
}

export const useTable = () => {
    const {
        validType,
        roleType,
        deptTreeItems,
        handleUpdate,
        getLabelByValue
    } = useEnumStore((state) => state);

    const { total, list, loading, getList: getUserList } = useUserList();

    const [listParam, setListParam] = useImmer(new ListParam());

    const [editNsp, setEditNsp] = useImmer({
        open: false,
        id: undefined as undefined | number
    });

    const paginationConfig = usePaginationHelper(
        total,
        listParam.size,
        listParam.current,
        (page, pageSize) => {
            setListParam((draft) => {
                draft.current = page;
                draft.size = pageSize;
            });
            getList();
        }
    );

    const formatParam = (param: ListParam) => {
        return {
            ...param,
            valid:
                typeof param.status === 'number'
                    ? param.status === 1
                    : undefined
        };
    };

    const getList = () => {
        setListParam((draft) => {
            draft.timeStamp = Date.now();
        });
    };

    const reset = () => {
        setListParam(new ListParam());
        getList();
    };

    const handleEdit = (id?: number) => {
        setEditNsp((draft) => {
            draft.id = id;
            draft.open = true;
        });
    };

    const afterUpdate = () => {
        window.location.reload();
    }

    useEffect(() => {
        getUserList(formatParam(listParam));
    }, [listParam.timeStamp]);

    useEffect(() => {
        handleUpdate(['roleType', 'validType', 'deptTreeItems']);
    }, []);
    const columns: TableProps<UserListItem>['columns'] = [
        {
            title: 'ID',
            dataIndex: 'id'
        },
        {
            title: '用户名',
            dataIndex: 'username'
        },
        {
            title: '是否生效',
            render: (_, record) => <div>{record.valid ? '是' : '否'}</div>
        },
        {
            title: '角色',
            render: (_, record) => (
                <div>
                    {record.roleList && record.roleList.length > 0
                        ? getLabelByValue('roleType', record.roleList[0].id)
                        : '暂无'}
                </div>
            )
        },
        {
            title: '所属科室',
            render: (_, record) => (
                <div>
                    {record.departmentList && record.departmentList.length > 0
                        ? record.departmentList[0].name
                        : '暂无'}
                </div>
            )
        },
        {
            title: '更新时间',
            render: (_, record) => <div>{record.updateTime}</div>
        },
        {
            title: '操作',
            render: (_, record) => (
                <div className="flex-row jc-center">
                    <Button
                        type="primary"
                        onClick={() => handleEdit(record.id)}
                    >
                        编辑
                    </Button>
                </div>
            )
        }
    ];
    return {
        columns,
        editNsp,
        setEditNsp,
        loading,
        list,
        total,
        listParam,
        setListParam,
        validType,
        roleType,
        getList,
        reset,
        handleEdit,
        deptTreeItems,
        paginationConfig,
        afterUpdate
    };
};
